/*
 * @author zzr
 * @date: 2025/10/13  19:37
 * @description: 删除链表中的重复节点
 * 再一个排序的链表中，存在重复的节点，请删除重复的节点，返回整理后的链表的头结点
 */
public class Demo14 {

    public class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode deleteDuplication(ListNode pHead) {
        ListNode cur = pHead;
        ListNode newHead = new ListNode(-1);
        ListNode tempHead = newHead;

        while (cur != null) {
            if (cur.next != null && cur.val == cur.next.val) {
                while (cur.next != null && cur.val == cur.next.val) {
                    cur = cur.next; // 向后遍历查找看有多少个重复的节点
                }
                cur = cur.next; // 删除这些重复的节点
            } else {
                tempHead.next = cur;
                tempHead = tempHead.next;
                cur = cur.next;
            }
        }
        tempHead.next = null;
        return newHead.next;
    }
}
